<template>
  <div class="profile-page">
    <!-- 顶部横幅 -->
    <section class="hero-banner">
      <div class="banner-bg"></div>
      <div class="hero-content">
        <div class="avatar-section">
          <div class="avatar-wrap">
            <div class="avatar" @click="showAvatarDialog = true" :style="avatarStyle">
              {{ userInitial }}
              <div class="avatar-hover-mask">
                <span class="edit-text">修改头像</span>
              </div>
            </div>
            <div class="level-badge">Lv.{{ profile.level || 1 }}</div>
          </div>
        </div>
        <div class="user-info">
          <h1 class="username">{{ user?.username || '游客' }}</h1>
          <div class="user-title">{{ userTitle }}</div>
          <div class="exp-bar">
            <div class="exp-label">
              <span>经验值</span>
              <span class="exp-text">{{ profile.exp_total || 0 }} / {{ profile.next_level_exp || 100 }}</span>
            </div>
            <el-progress 
              :percentage="expPercentage" 
              :stroke-width="12" 
              :show-text="false"
              color="#f6d58e"
            />
          </div>
          <div class="user-stats">
            <div class="stat-item">
              <span class="stat-icon">✅</span>
              <span class="stat-value">{{ profile.quests_done || 0 }}</span>
              <span class="stat-label">完成任务</span>
            </div>
            <div class="stat-item">
              <span class="stat-icon">🔓</span>
              <span class="stat-value">{{ achievementCount }}</span>
              <span class="stat-label">获得成就</span>
            </div>
            <div class="stat-item">
              <span class="stat-icon">📅</span>
              <span class="stat-value">{{ joinedDays }}</span>
              <span class="stat-label">加入天数</span>
            </div>
          </div>
        </div>
      </div>
    </section>

    <!-- 主内容区 -->
    <div class="profile-main">
      <div class="main-container">
        <!-- 左侧统计卡片 -->
        <aside class="stats-sidebar">
          <!-- 核心数据 -->
          <el-card class="stats-card modern-vintage" shadow="hover">
            <template #header>
              <div class="card-title">📊 我的成长</div>
            </template>
            <div class="stat-row">
              <span class="label">等级</span>
              <span class="value">Lv.{{ profile.level || 1 }}</span>
            </div>
            <div class="stat-row">
              <span class="label">总经验</span>
              <span class="value">{{ profile.exp_total || 0 }}</span>
            </div>
            <div class="stat-row">
              <span class="label">完成任务</span>
              <span class="value">{{ profile.quests_done || 0 }}</span>
            </div>
            <div class="stat-row">
              <span class="label">获得成就</span>
              <span class="value gold">{{ achievementCount }}</span>
            </div>
          </el-card>

          <!-- 最新成就 -->
          <el-card class="stats-card modern-vintage" shadow="hover" v-if="recentAchievements.length > 0">
            <template #header>
              <div class="card-title">🏆 最新成就</div>
            </template>
            <div class="achievement-item" v-for="ach in recentAchievements" :key="ach.id">
              <div class="ach-icon">🥇</div>
              <div class="ach-info">
                <div class="ach-title">{{ ach.title }}</div>
                <div class="ach-time">{{ formatTime(ach.earned_at) }}</div>
              </div>
            </div>
          </el-card>
        </aside>

        <!-- 右侧内容区 -->
        <main class="content-area">
          <el-tabs v-model="activeTab" class="profile-tabs">
            <!-- 数据统计 -->
            <el-tab-pane label="数据统计" name="stats">
              <div class="tab-content">
                <el-skeleton v-if="loading.stats" :rows="3" animated />
                <div v-else class="stats-grid">
                  <div class="stat-card">
                    <div class="stat-card-icon">💬</div>
                    <div class="stat-card-value">{{ userStats.chat_sessions || 0 }}</div>
                    <div class="stat-card-label">对话会话</div>
                  </div>
                  <div class="stat-card">
                    <div class="stat-card-icon">📝</div>
                    <div class="stat-card-value">{{ userStats.total_messages || 0 }}</div>
                    <div class="stat-card-label">消息总数</div>
                  </div>
                  <div class="stat-card">
                    <div class="stat-card-icon">🎨</div>
                    <div class="stat-card-value">{{ userStats.images_generated || 0 }}</div>
                    <div class="stat-card-label">生成图像</div>
                  </div>
                  <div class="stat-card">
                    <div class="stat-card-icon">🌟</div>
                    <div class="stat-card-value">{{ userStats.favorite_images || 0 }}</div>
                    <div class="stat-card-label">收藏图像</div>
                  </div>
                  <div class="stat-card">
                    <div class="stat-card-icon">🗺️</div>
                    <div class="stat-card-value">{{ profile.nodes_unlocked || 0 }}</div>
                    <div class="stat-card-label">解锁节点</div>
                  </div>
                  <div class="stat-card">
                    <div class="stat-card-icon">✅</div>
                    <div class="stat-card-value">{{ profile.quests_done || 0 }}</div>
                    <div class="stat-card-label">完成任务</div>
                  </div>
                </div>
              </div>
            </el-tab-pane>

            <!-- 成就徽章 -->
            <el-tab-pane label="成就徽章" name="achievements">
              <div class="tab-content">
                <el-skeleton v-if="loading.achievements" :rows="3" animated />
                <el-empty v-else-if="achievements.length === 0" description="暂无成就" />
                <div v-else class="achievements-grid">
                  <div v-for="ach in achievements" :key="ach.id" class="achievement-card" :class="{ earned: ach.earned }">
                    <div class="ach-badge">🏅</div>
                    <div class="ach-name">{{ ach.title }}</div>
                    <div class="ach-desc">{{ ach.description || '—' }}</div>
                    <el-tag v-if="ach.earned" size="small" type="success">已获得</el-tag>
                    <el-tag v-else size="small" type="info">未获得</el-tag>
                  </div>
                </div>
              </div>
            </el-tab-pane>

            <!-- 个人信息 -->
            <el-tab-pane label="个人信息" name="info">
              <div class="tab-content">
                <div class="info-section">
                  <h3 class="section-title">基本信息</h3>
                  <div class="info-cards">
                    <div class="info-card">
                      <div class="info-label">👤 用户名</div>
                      <div class="info-value-row">
                        <span class="info-value">{{ user?.username || '—' }}</span>
                        <el-button 
                          link 
                          type="primary" 
                          size="small" 
                          @click="showUsernameDialog = true"
                          v-if="!user?.is_guest"
                        >
                          修改
                        </el-button>
                      </div>
                    </div>
                    <div class="info-card">
                      <div class="info-label">🔑 密码</div>
                      <div class="info-value-row">
                        <span class="info-value">••••••••</span>
                        <el-button 
                          link 
                          type="primary" 
                          size="small" 
                          @click="showPasswordDialog = true"
                          v-if="!user?.is_guest"
                        >
                          修改
                        </el-button>
                      </div>
                    </div>
                    <div class="info-card">
                      <div class="info-label">🆔 用户ID</div>
                      <div class="info-value">{{ user?.id || '—' }}</div>
                    </div>
                    <div class="info-card">
                      <div class="info-label">📌 账号类型</div>
                      <div class="info-value">
                        <el-tag :type="user?.is_guest ? 'info' : 'success'" size="small">
                          {{ user?.is_guest ? '游客' : '正式用户' }}
                        </el-tag>
                      </div>
                    </div>
                    <div class="info-card">
                      <div class="info-label">✨ 账号状态</div>
                      <div class="info-value">
                        <el-tag :type="user?.is_active ? 'success' : 'warning'" size="small">
                          {{ user?.is_active ? '正常' : '未激活' }}
                        </el-tag>
                      </div>
                    </div>
                    <div class="info-card">
                      <div class="info-label">📅 注册时间</div>
                      <div class="info-value">{{ formatDate(user?.created_at) }}</div>
                    </div>
                    <div class="info-card">
                      <div class="info-label">⭐ 当前等级</div>
                      <div class="info-value">Lv.{{ profile.level || 1 }} - {{ userTitle }}</div>
                    </div>
                    <div class="info-card">
                      <div class="info-label">🎯 总经验值</div>
                      <div class="info-value">{{ profile.exp_total || 0 }} EXP</div>
                    </div>
                  </div>
                </div>
              </div>
            </el-tab-pane>
          </el-tabs>
        </main>
      </div>
    </div>

    <!-- 修改头像对话框 -->
    <el-dialog 
      v-model="showAvatarDialog" 
      title="修改头像" 
      width="480px"
      :close-on-click-modal="false"
      class="modern-dialog"
    >
      <div class="avatar-upload-section">
        <div class="current-avatar">
          <div class="preview-avatar" :style="avatarStyle">
            {{ userInitial }}
          </div>
          <p class="preview-label">当前头像</p>
        </div>
        
        <el-tabs v-model="avatarUploadTab" class="avatar-tabs">
          <el-tab-pane label="上传图片" name="upload">
            <div class="upload-area">
              <el-upload
                ref="uploadRef"
                :auto-upload="false"
                :show-file-list="false"
                :on-change="handleAvatarSelect"
                accept="image/jpeg,image/png,image/gif,image/webp"
                drag
              >
                <div class="upload-content">
                  <el-icon class="upload-icon"><Upload /></el-icon>
                  <div class="upload-text">点击或拖拽图片到此处</div>
                  <div class="upload-hint">支持 JPG、PNG、GIF、WEBP 格式，最大2MB</div>
                </div>
              </el-upload>
              <div v-if="avatarFile" class="file-info">
                <span>已选择: {{ avatarFile.name }}</span>
                <el-button link type="danger" @click="avatarFile = null">删除</el-button>
              </div>
            </div>
          </el-tab-pane>
          
          <el-tab-pane label="在线图片" name="url">
            <el-form label-width="80px">
              <el-form-item label="图片URL">
                <el-input 
                  v-model="avatarUrlForm.avatar_url" 
                  placeholder="请输入图片链接 (http:// 或 https://)"
                  clearable
                />
              </el-form-item>
              <el-form-item>
                <el-button 
                  type="primary" 
                  @click="handleAvatarUrlUpdate" 
                  :loading="loading.avatar"
                  style="width: 100%"
                >
                  设置为URL头像
                </el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </div>
      
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="handleDeleteAvatar" type="danger" plain v-if="user?.avatar_url">
            删除头像
          </el-button>
          <el-button @click="showAvatarDialog = false">取消</el-button>
          <el-button 
            type="primary" 
            @click="handleAvatarUpload" 
            :loading="loading.avatar"
            v-if="avatarUploadTab === 'upload' && avatarFile"
          >
            上传
          </el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 修改用户名对话框 -->
    <el-dialog 
      v-model="showUsernameDialog" 
      title="修改用户名" 
      width="480px"
      :close-on-click-modal="false"
      class="modern-dialog"
    >
      <el-form :model="usernameForm" label-width="100px">
        <el-form-item label="当前用户名">
          <el-input :value="user?.username" disabled />
        </el-form-item>
        <el-form-item label="新用户名">
          <el-input 
            v-model="usernameForm.new_username" 
            placeholder="请输入新用户名"
            maxlength="50"
            show-word-limit
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="showUsernameDialog = false">取消</el-button>
          <el-button 
            type="primary" 
            @click="handleUsernameChange" 
            :loading="loading.username"
          >
            确定修改
          </el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 修改密码对话框 -->
    <el-dialog 
      v-model="showPasswordDialog" 
      title="修改密码" 
      width="480px"
      :close-on-click-modal="false"
      class="modern-dialog"
    >
      <el-form :model="passwordForm" label-width="100px">
        <el-form-item label="旧密码">
          <el-input 
            v-model="passwordForm.old_password" 
            type="password" 
            show-password 
            placeholder="请输入旧密码"
          />
        </el-form-item>
        <el-form-item label="新密码">
          <el-input 
            v-model="passwordForm.new_password" 
            type="password" 
            show-password 
            placeholder="请输入新密码"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="showPasswordDialog = false">取消</el-button>
          <el-button 
            type="primary" 
            @click="handlePasswordChange" 
            :loading="loading.password"
          >
            确定修改
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Upload } from '@element-plus/icons-vue'
import api, { authApi } from '@/api/http'
import { exploreApi } from '@/api/explore'
import { removeToken } from '@/utils/auth'

const router = useRouter()
const activeTab = ref('stats')
const user = ref(null)
const profile = ref({
  level: 1,
  exp_total: 0,
  next_level_exp: 100,
  achievement_points: 0,
  quests_done: 0,
  nodes_unlocked: 0
})
const achievements = ref([])
const userStats = ref({
  chat_sessions: 0,
  total_messages: 0,
  images_generated: 0,
  favorite_images: 0
})
const showPasswordDialog = ref(false)
const showUsernameDialog = ref(false)
const showAvatarDialog = ref(false)
const avatarUploadTab = ref('upload')
const avatarFile = ref(null)
const uploadRef = ref(null)
const passwordForm = ref({
  old_password: '',
  new_password: ''
})
const usernameForm = ref({
  new_username: ''
})
const avatarUrlForm = ref({
  avatar_url: ''
})

const loading = ref({
  user: false,
  achievements: false,
  stats: false,
  password: false,
  username: false,
  avatar: false
})

// 计算属性
const avatarStyle = computed(() => {
  if (user.value?.avatar_url) {
    // 如果是完整 http/https 链接，直接使用；否则当作相对路径交给当前站点处理
    const avatarUrl = user.value.avatar_url.startsWith('http')
      ? user.value.avatar_url
      : user.value.avatar_url
    return {
      backgroundImage: `url(${avatarUrl})`,
      backgroundSize: 'cover',
      backgroundPosition: 'center',
      color: 'transparent'
    }
  }
  return {}
})

const userInitial = computed(() => {
  if (user.value?.avatar_url) return ''
  return user.value?.username?.charAt(0)?.toUpperCase() || 'U'
})

const userTitle = computed(() => {
  const level = profile.value.level || 1
  if (level >= 10) return '进士'
  if (level >= 7) return '举人'
  if (level >= 5) return '秀才'
  if (level >= 3) return '书生'
  return '蒙童'
})

const expPercentage = computed(() => {
  const current = profile.value.exp_total || 0
  const next = profile.value.next_level_exp || 100
  return Math.min(100, Math.round((current / next) * 100))
})

const joinedDays = computed(() => {
  if (!user.value?.created_at) return 0
  const created = new Date(user.value.created_at)
  const now = new Date()
  const diff = now - created
  return Math.floor(diff / (1000 * 60 * 60 * 24))
})

const achievementCount = computed(() => {
  return achievements.value.filter(a => a.earned).length
})

const recentAchievements = computed(() => {
  return achievements.value
    .filter(a => a.earned)
    .sort((a, b) => new Date(b.earned_at) - new Date(a.earned_at))
    .slice(0, 3)
})

// 加载数据
const loadUserData = async () => {
  loading.value.user = true
  try {
    const token = localStorage.getItem('ancientseeker_token')
    console.log('🔑 Token 状态:', token ? `存在 (${token.substring(0, 20)}...)` : '不存在')
    
    const response = await authApi.me()
    console.log('👤 用户数据响应:', response)
    user.value = response.data
    console.log('✅ 用户ID:', user.value.id, '用户名:', user.value.username)
  } catch (err) {
    console.error('❌ 加载用户信息失败:', err)
    console.error('错误详情:', err.response?.data)
    ElMessage.error('加载用户信息失败，请重新登录')
  } finally {
    loading.value.user = false
  }
}

const loadProfile = async () => {
  try {
    const response = await exploreApi.userSummary()
    console.log('🗺️ 探索数据响应:', response)
    profile.value = response.data
    console.log('🎯 等级:', profile.value.level, '经验:', profile.value.exp_total, '任务:', profile.value.quests_done, '节点:', profile.value.nodes_unlocked)
  } catch (err) {
    console.error('❌ 加载探索数据失败:', err)
    console.error('错误详情:', err.response?.data)
    // 不显示错误消息，因为可能是新用户还没有探索数据
  }
}

const loadAchievements = async () => {
  loading.value.achievements = true
  try {
    const response = await exploreApi.achievements()
    console.log('成就数据响应:', response)
    achievements.value = response.data
  } catch (err) {
    console.error('加载成就失败:', err)
    console.error('错误详情:', err.response?.data)
  } finally {
    loading.value.achievements = false
  }
}

const loadUserStats = async () => {
  loading.value.stats = true
  try {
    // 获取对话统计
    const chatResponse = await api.get('/api/chat/sessions')
    console.log('对话数据响应:', chatResponse)
    const sessions = chatResponse.data?.sessions || []
    userStats.value.chat_sessions = sessions.length
    userStats.value.total_messages = sessions.reduce((sum, s) => sum + (s.message_count || 0), 0)
    
    // 获取图像统计
    const imageResponse = await api.get('/api/ai/images')
    console.log('图像数据响应:', imageResponse)
    const images = Array.isArray(imageResponse.data) ? imageResponse.data : []
    userStats.value.images_generated = images.length
    userStats.value.favorite_images = images.filter(img => img.is_favorite).length
  } catch (err) {
    console.error('加载统计数据失败:', err)
    console.error('错误详情:', err.response?.data)
    // 不显示错误，使用默认值0
  } finally {
    loading.value.stats = false
  }
}

// 工具函数
const formatDate = (dateStr) => {
  if (!dateStr) return '—'
  const date = new Date(dateStr)
  return date.toLocaleDateString('zh-CN')
}

const formatTime = (dateStr) => {
  if (!dateStr) return '—'
  const date = new Date(dateStr)
  return date.toLocaleString('zh-CN')
}

const formatTimeAgo = (date) => {
  const now = new Date()
  const diff = now - date
  const hours = Math.floor(diff / (1000 * 60 * 60))
  const days = Math.floor(diff / (1000 * 60 * 60 * 24))
  
  if (hours < 1) return '刚刚'
  if (hours < 24) return `${hours}小时前`
  if (days < 7) return `${days}天前`
  return formatDate(date)
}

// 操作函数
const handleAvatarSelect = (file) => {
  // 验证文件类型
  const allowedTypes = ['image/jpeg', 'image/png', 'image/gif', 'image/webp']
  if (!allowedTypes.includes(file.raw.type)) {
    ElMessage.error('只支持 JPG、PNG、GIF、WEBP 格式的图片')
    return
  }
  
  // 验证文件大小（2MB）
  if (file.raw.size > 2 * 1024 * 1024) {
    ElMessage.error('图片大小不能超过2MB')
    return
  }
  
  avatarFile.value = file.raw
}

const handleAvatarUpload = async () => {
  if (!avatarFile.value) {
    ElMessage.warning('请先选择图片')
    return
  }
  
  loading.value.avatar = true
  try {
    const formData = new FormData()
    formData.append('file', avatarFile.value)
    
    const response = await api.post('/api/auth/avatar/upload', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
    
    user.value = response.data
    ElMessage.success('头像上传成功')
    showAvatarDialog.value = false
    avatarFile.value = null
  } catch (err) {
    ElMessage.error(err.response?.data?.detail || '头像上传失败')
  } finally {
    loading.value.avatar = false
  }
}

const handleAvatarUrlUpdate = async () => {
  if (!avatarUrlForm.value.avatar_url || !avatarUrlForm.value.avatar_url.trim()) {
    ElMessage.warning('请输入图片URL')
    return
  }
  
  loading.value.avatar = true
  try {
    const response = await api.put('/api/auth/avatar/url', avatarUrlForm.value)
    user.value = response.data
    ElMessage.success('头像设置成功')
    showAvatarDialog.value = false
    avatarUrlForm.value.avatar_url = ''
  } catch (err) {
    ElMessage.error(err.response?.data?.detail || '头像设置失败')
  } finally {
    loading.value.avatar = false
  }
}

const handleDeleteAvatar = async () => {
  loading.value.avatar = true
  try {
    await api.delete('/api/auth/avatar')
    user.value.avatar_url = null
    ElMessage.success('头像已删除')
    showAvatarDialog.value = false
  } catch (err) {
    ElMessage.error(err.response?.data?.detail || '删除失败')
  } finally {
    loading.value.avatar = false
  }
}

const handleUsernameChange = async () => {
  if (!usernameForm.value.new_username || !usernameForm.value.new_username.trim()) {
    ElMessage.warning('请输入新用户名')
    return
  }
  
  if (usernameForm.value.new_username === user.value?.username) {
    ElMessage.warning('新用户名与当前用户名相同')
    return
  }
  
  loading.value.username = true
  try {
    await api.put('/api/auth/username', { new_username: usernameForm.value.new_username.trim() })
    ElMessage.success('用户名修改成功')
    showUsernameDialog.value = false
    usernameForm.value.new_username = ''
    // 重新加载用户数据
    await loadUserData()
  } catch (err) {
    ElMessage.error(err.response?.data?.detail || '用户名修改失败')
  } finally {
    loading.value.username = false
  }
}

const handlePasswordChange = async () => {
  if (!passwordForm.value.old_password || !passwordForm.value.new_password) {
    ElMessage.warning('请填写完整信息')
    return
  }
  
  loading.value.password = true
  try {
    await api.put('/api/auth/password', passwordForm.value)
    ElMessage.success('密码修改成功')
    showPasswordDialog.value = false
    passwordForm.value = { old_password: '', new_password: '' }
  } catch (err) {
    ElMessage.error(err.response?.data?.detail || '密码修改失败')
  } finally {
    loading.value.password = false
  }
}

// 生命周期
onMounted(() => {
  loadUserData()
  loadProfile()
  loadAchievements()
  loadUserStats()
})
</script>

<style scoped lang="scss">
.profile-page {
  min-height: 100vh;
  padding-bottom: 40px;
}

/* 顶部横幅 */
.hero-banner {
  position: relative;
  background: linear-gradient(135deg, #e8576b 0%, #f4a261 50%, #fbc2a3 100%);
  padding: 40px 20px;
  overflow: hidden;
}

.banner-bg {
  position: absolute;
  inset: 0;
  background: 
    radial-gradient(circle at 20% 30%, rgba(255,255,255,0.15) 0%, transparent 50%),
    radial-gradient(circle at 80% 70%, rgba(255,255,255,0.1) 0%, transparent 50%);
  opacity: 0.6;
}

.hero-content {
  position: relative;
  max-width: 1240px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 32px;
}

.avatar-section {
  flex-shrink: 0;
}

.avatar-wrap {
  position: relative;
  width: 100px;
  height: 100px;
}

.avatar {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 40px;
  font-weight: 600;
  border: 4px solid rgba(255,255,255,0.9);
  box-shadow: 0 8px 24px rgba(0,0,0,0.25);
  cursor: pointer;
  transition: all 0.3s ease;
  overflow: hidden;
  position: relative;
}

.avatar:hover {
  transform: scale(1.05);
  box-shadow: 0 12px 32px rgba(0,0,0,0.35);
}

.avatar-hover-mask {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.avatar:hover .avatar-hover-mask {
  opacity: 1;
}

.edit-text {
  color: #fff;
  font-size: 14px;
  font-weight: 500;
  text-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

.level-badge {
  position: absolute;
  bottom: -5px;
  right: -5px;
  background: #f6d58e;
  color: #8b4513;
  padding: 4px 12px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  border: 2px solid #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}

.user-info {
  flex: 1;
  color: #fff;
}

.username {
  margin: 0 0 4px 0;
  font-size: 28px;
  font-weight: 600;
  text-shadow: 0 2px 8px rgba(0,0,0,0.2);
}

.user-title {
  font-size: 14px;
  opacity: 0.9;
  margin-bottom: 16px;
}

.exp-bar {
  margin-bottom: 20px;
}

.exp-label {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
  font-size: 13px;
}

.exp-text {
  font-weight: 600;
}

.user-stats {
  display: flex;
  gap: 32px;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.stat-icon {
  font-size: 24px;
}

.stat-value {
  font-size: 20px;
  font-weight: 600;
}

.stat-label {
  font-size: 12px;
  opacity: 0.9;
}

/* 主内容区 */
.profile-main {
  max-width: 1240px;
  margin: 0 auto;
  padding: 30px 20px 0;
  position: relative;
  z-index: 1;
}

.main-container {
  display: grid;
  grid-template-columns: 280px 1fr;
  gap: 20px;
}

/* 侧边栏 */
.stats-sidebar {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.stats-card {
  border-radius: 14px;
  border: 1px solid #eadfcd;
  background: linear-gradient(180deg, #fff, #faf6ef);
}

.stats-card :deep(.el-card__header) {
  padding: 14px 16px;
  border-bottom: 1px solid #eadfcd;
}

.card-title {
  font-weight: 600;
  font-size: 15px;
  color: #2c3e50;
}

.stat-row {
  display: flex;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px dashed #f0e8d8;
}

.stat-row:last-child {
  border-bottom: none;
}

.stat-row .label {
  color: #666;
  font-size: 14px;
}

.stat-row .value {
  font-weight: 600;
  color: #2c3e50;
}

.stat-row .value.gold {
  color: #c8102e;
}

.achievement-item {
  display: flex;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px dashed #f0e8d8;
}

.achievement-item:last-child {
  border-bottom: none;
}

.ach-icon {
  font-size: 24px;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff8e6;
  border-radius: 8px;
}

.ach-info {
  flex: 1;
}

.ach-title {
  font-weight: 600;
  font-size: 13px;
  margin-bottom: 2px;
}

.ach-time {
  font-size: 11px;
  color: #999;
}

/* 内容区 */
.content-area {
  background: #fff;
  border-radius: 14px;
  padding: 20px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.08);
}

.profile-tabs :deep(.el-tabs__header) {
  margin-bottom: 20px;
}

.tab-content {
  padding: 10px 0;
}

.section-title {
  margin: 0 0 16px 0;
  font-size: 16px;
  font-weight: 600;
  color: #2c3e50;
}

/* 数据统计网格 */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
}

.stat-card {
  background: linear-gradient(135deg, #faf6ef 0%, #fff 100%);
  border: 2px solid #f6d58e;
  border-radius: 12px;
  padding: 24px;
  text-align: center;
  transition: all 0.3s ease;
  cursor: default;
}

.stat-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(139, 69, 19, 0.15);
  border-color: #e8c679;
}

.stat-card-icon {
  font-size: 42px;
  margin-bottom: 12px;
}

.stat-card-value {
  font-size: 32px;
  font-weight: 700;
  color: #c8102e;
  margin-bottom: 8px;
}

.stat-card-label {
  font-size: 14px;
  color: #8b4513;
  font-weight: 500;
}

/* 个人信息卡片 */
.info-section {
  .section-title {
    margin-bottom: 24px;
  }
}

.info-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 16px;
}

.info-card {
  background: linear-gradient(135deg, #faf6ef 0%, #fff 100%);
  border: 1px solid #f6d58e;
  border-radius: 12px;
  padding: 20px;
  transition: all 0.3s ease;
}

.info-card:hover {
  border-color: #e8c679;
  box-shadow: 0 4px 12px rgba(139, 69, 19, 0.1);
}

.info-label {
  font-size: 13px;
  color: #8b4513;
  font-weight: 500;
  margin-bottom: 8px;
}

.info-value {
  font-size: 16px;
  color: #333;
  font-weight: 500;
}

.info-value-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.info-value-row .info-value {
  flex: 1;
}

/* 成就网格 */
.achievements-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
}

.achievement-card {
  padding: 16px;
  border: 1px solid #eadfcd;
  border-radius: 12px;
  background: linear-gradient(180deg, #fff, #faf6ef);
  text-align: center;
  transition: all 0.3s;
}

.achievement-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 18px rgba(0,0,0,0.12);
}

.achievement-card.earned {
  border-color: #f6d58e;
  background: linear-gradient(180deg, #fffdf7, #fffaf0);
}

.ach-badge {
  font-size: 40px;
  margin-bottom: 8px;
}

.ach-name {
  font-weight: 600;
  margin-bottom: 6px;
  color: #2c3e50;
}

.ach-desc {
  font-size: 12px;
  color: #666;
  margin-bottom: 10px;
}

/* 对话框样式 */
.modern-dialog :deep(.el-dialog__header) {
  background: linear-gradient(135deg, #c8102e 0%, #e8576b 100%);
  color: #fff;
  padding: 20px;
  margin: 0;
}

.modern-dialog :deep(.el-dialog__title) {
  color: #fff;
  font-weight: 600;
}

.modern-dialog :deep(.el-dialog__body) {
  padding: 30px;
}

.modern-dialog :deep(.el-form-item__label) {
  color: #8b4513;
  font-weight: 500;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

/* 头像上传样式 */
.avatar-upload-section {
  .current-avatar {
    text-align: center;
    margin-bottom: 24px;
    
    .preview-avatar {
      width: 120px;
      height: 120px;
      border-radius: 50%;
      background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      color: #fff;
      display: inline-flex;
      align-items: center;
      justify-content: center;
      font-size: 48px;
      font-weight: 600;
      border: 4px solid #f6d58e;
      box-shadow: 0 8px 24px rgba(0,0,0,0.2);
      overflow: hidden;
    }
    
    .preview-label {
      margin-top: 12px;
      color: #666;
      font-size: 14px;
    }
  }
}

.upload-area {
  .upload-content {
    padding: 40px;
    text-align: center;
    
    .upload-icon {
      font-size: 64px;
      color: #c8102e;
      margin-bottom: 16px;
    }
    
    .upload-text {
      font-size: 16px;
      color: #333;
      margin-bottom: 8px;
    }
    
    .upload-hint {
      font-size: 13px;
      color: #999;
    }
  }
  
  .file-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 16px;
    padding: 12px;
    background: #f5f5f5;
    border-radius: 8px;
  }
}

.avatar-tabs :deep(.el-tabs__header) {
  margin-bottom: 20px;
}

/* 响应式 */
@media (max-width: 960px) {
  .main-container {
    grid-template-columns: 1fr;
  }

  .hero-content {
    flex-direction: column;
    text-align: center;
  }

  .user-stats {
    justify-content: center;
  }

  .stats-sidebar {
    display: none;
  }
}

@media (max-width: 600px) {
  .achievements-grid {
    grid-template-columns: 1fr;
  }
  
  .user-stats {
    gap: 20px;
  }
}

/* 现代古风融合 */
.modern-vintage {
  box-shadow: 0 8px 18px rgba(0,0,0,.06);
}
</style>